package cn.mxz.robots.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;

public class RankUtil
{
	// 排名缓存池
	private static final HashMap<String, Integer> rankCachePool = new HashMap<String, Integer>();
	// 分数缓存池
	private static final HashMap<String, Integer> scoreCachePool = new HashMap<String, Integer>();

	private static Logger LOG = Logger.getLogger(RankUtil.class);

	public static void set(TreeMap<Integer, Integer> scoreRange, String type, Integer provinceId)
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(provinceId);

		GaoKaoRankService rankService = new GaoKaoRankService();

		Set<Integer> scores = scoreRange.descendingKeySet();
		Iterator<Integer> si = scores.iterator();

		GaoKaoRank preRank = null;

		while (si.hasNext())
		{
			Integer score = si.next();
			Integer count = scoreRange.get(score);

			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(score);
			rank.setCount(count);
			rank.setType(type);
			rank.setGaoKaoProvince(province);

			if (preRank == null)
			{
				rank.setRanking(1);
			}
			else
			{
				rank.setRanking(preRank.getRanking() + preRank.getCount());
			}

			preRank = rank;

			rankService.save(rank);

			LOG.info("RankUtil class method for[set] provinceId:" + provinceId + " rank:" + rank);
		}

	}

	public static void set(ArrayList<GaoKaoRank> ranks, Integer provinceId)
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(provinceId);

		Iterator<GaoKaoRank> ranksIterator = ranks.iterator();

		while (ranksIterator.hasNext())
		{
			GaoKaoRank rank = ranksIterator.next();
			rank.setGaoKaoProvince(province);

			GaoKaoRankService rankService = new GaoKaoRankService();

			rankService.save(rank);
			System.out.println(rank);
		}
	}

	public static Integer get(Integer provinceId, String score, String type)
	{
		Integer scoreInt = Integer.parseInt(score);
		String key = provinceId + "_" + scoreInt + "_" + type;
		Integer value = rankCachePool.get(key);

		if (value != null)
			return value;
		else
		{
			GaoKaoRankService service = new GaoKaoRankService();
			Integer rank = service.get(provinceId, scoreInt, type);

			scoreCachePool.put(key, rank);

			LOG.info("RankUtil class method for[get] load score:" + score);

			return rank;
		}
	}

	public static Integer getTransformScore(Integer provinceId, Integer ranking, String type)
	{
		String key = provinceId + "_" + ranking + "_" + type;
		Integer value = scoreCachePool.get(key);

		if (value != null)
			return value;
		else
		{
			LOG.info("load getTransformScore. ProvinceID = " + provinceId + " Ranking = " + ranking + " Type = " + type);

			GaoKaoRankService service = new GaoKaoRankService();
			Integer score = service.getTransformScore(provinceId, ranking, type);
			scoreCachePool.put(key, score);

			return score;
		}
	}
}
